<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('元素条件判断')"/>
    <th:block th:include="include :: bootstrap-select-css"/>
    <link th:href="@{/css/common.css}" rel="stylesheet"/>
    <link th:href="@{/css/font-awesome.min.css}" rel="stylesheet"/>
    <link th:href="@{/css/process_arrangement.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
<input type = "hidden" name="pageId" th:value="${pageId}">
<input type = "hidden" name="pageImageId" th:value="${pageImageId}">
<input type = "hidden" name="showId" value="">
<div class="wrapper wrapper-content page-element">
    <div class="header">
        <h4 class="fz16">[[${pageName}]]</h4>
    </div>
    <div class="content">
        <div class="content-box left-part pull-left">
            <ul class="nav nav-tabs navtabs" role="tablist">
                <li>
                    <a href="#byProcessBranchName" role="tab" data-toggle="tab">元素条件判断</a></li>
                </li>
            </ul>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="byProcessBranchName">
                    <div class="no-line-tree well">
                        <ul id="treeRootByName"></ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="content-box right-part pull-left">
        </div>
    </div>
</div>
<div class="modal inmodal fade process-conditions-model" id="processConditions" tabindex="-1" role="dialog"
     aria-hidden="false">
    <div class="modal-dialog modal-lg" style="width: 1400px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span>
                </button>
                <h4 class="modal-title pull-left">元素判断条件</h4>
            </div>
            <input type="hidden" name="pagImageElementRefId"/>
            <input type="hidden" name="conditionId"/>
            <div class="modal-body">
                <div style="display: flex;align-items: center;line-height: 31px;">
                    <label style="font-size: 14px;margin-right: 6px">页面元素div02：</label>
                    <select  name="elementConditionType" class="form-control">
                        <option value="1">展示</option>
                        <option value="2">不展示</option>
                        <option value="3">禁用</option>
                    </select>
                </div>
                <div class="tab-content">
                    <div role="tabpanel" class="tab-pane active">
                        <table class="table" id="conditionTable">
                            <thead>
                            <tr>
                                <th></th>
                                <th>序号</th>
                                <th>模式名</th>
                                <th>分组</th>
                                <th>来源</th>
                                <th>接口</th>
                                <th>对象类型</th>
                                <th>判断条件</th>
                                <th>约定值</th>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="add-condition-btn" onclick="addCondition()"><i class="fa fa-plus" style="margin-right: 5px;"></i>新增条件</div>
                <textarea placeholder="输入需求故事描述" class="description"></textarea>
            </div>
            <div class="modal-footer">
                <div>
                    <span class="pull-left">条件公式</span>
                    <select class="form-control join-condition">
                        <option value="and" selected="selected">AND</option>
                        <option value="or">OR</option>
                    </select>
                </div>
                <div class="groupList"></div>
                <button type="button" class="btn btn-white" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-success save">确定</button>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-select-js"/>
<script id="pageElementTpl" type="text/html">
    {{# layui.each(d.data, function(index, a){ }}
    <li  class="detail" data-id="{{a.id}}" style="cursor: pointer;">
        <div>
            <span class="badge badge-primary bg-yellow">{{index+1}}</span>
            <span class="content-txt">{{a.pageElementName}}</span>
             <a class="addCondition" data-elementrefid="{{a.id}}" data-name="{{a.pageElementName}}">添加判断条件</a>
        </div>
        <ul class="conditionList{{a.id}} conditionList">
            {{# layui.each(a.pageImageElementConditionList, function(aindex, b){ }}
            <li data-pindex="{{index}}" data-index="{{aindex}}">
                <div>
                    <span class="badge badge-primary bg-blue">{{index+1}}.{{aindex+1}}</span>
                    <span class="content-txt">{{a.pageElementName}}  {{elementConditionTypes[b.elementConditionType]}}</span>
                    <a class="editCondition"
                       data-elementrefid="{{a.id}}"
                       data-id="{{b.id}}"
                       data-name="{{a.pageElementName}}"
                       data-joincondition="{{b.joinCondition}}"
                       data-state="{{b.name}}"
                    ><i class="fa fa-edit"></i>修改</a>&nbsp;
                    <a class="page-element-conditions-delete" id="{{b.id}}"><i class="fa fa-remove"></i>删除</a>
                </div>
            </li>
            {{# }); }}
        </ul>
    </li>
    {{# }); }}
</script>
<script id="addConditionTpl" type="text/html">
    <tr>
        <td class="delCondition"><img th:src="@{/img/Icon-jianshao.png}" class="icon-jian"></td>
        <td>1</td>
        <td>
            <select class="form-control branchConditionDetailDictCode-select" name="branchConditionDetailDictCode">
                <option value="1" selected="selected">字段</option>
                <option value="2">数据集条数</option>
                <option value="3">出参</option>
            </select>
        </td>
        <td>
            <select class="form-control group-select">
                <option value="1" selected="selected">组1</option>
                <option value="2">组2</option>
                <option value="3">组3</option>
                <option value="4">组4</option>
                <option value="5">组5</option>
            </select>
        </td>
        <td>
            <select  name="sourceType" class="form-control source-select" style="display: inline">
                <option value="0">--选择来源--</option>
                <option value="1" {{# if(1==d.sourceType){ }}selected{{# }}}>来源接口</option>
                <option value="3" {{# if(3==d.sourceType){ }}selected{{# }}}>来源前端本地</option>
            </select>
        </td>
        <td>
            <select class="form-control interface-select">
                <option value="0">--请选择--</option>
            </select>
        </td>
        <td style="display: flex">
            <select class="form-control obj1-select">
                <option value="0" selected="selected">--请选择--</option>
                <option value="1">字段</option>
                <option value="2" style="display: none">数据集</option>
                <option value="3">数据集字段</option>
            </select>
            <div class="ObjSelect" style="display: flex"></div>
        </td>
        <td>
            <select class="form-control condition-select">
                {{# layui.each(d.conditionDictList, function(index, a){ }}
                <option value="{{a.dictValue}}">{{a.dictLabel}}</option>
                {{# }); }}
            </select>
        </td>
        <td class="contractValueTd">
            <input class="form-control contractValue" value=""/>
        </td>
    </tr>
    </tr>
</script>
<script id="groupInfoTpl" type="text/html">
    {{# layui.each(d, function(index, a){ }}
    <div class="dashed-p">
        <span class="pull-left">组{{a.branchConditionGroupId}}</span>
        <select class="form-control select-group{{a.branchConditionGroupId}}">
            <option value="and" {{#if(a.joinCondition=='and'){}}selected{{# }}}>AND</option>
            <option value="or" {{#if(a.joinCondition=='or'){}}selected{{# }}}>OR</option>
        </select>
    </div>
    {{# }); }}
</script>
<script id="interfaceOptionTpl" type="text/template">
    <select  name="sourceInterfaceId" class="form-control select-{{d.type}} interface-select" style="display: inline">
        <option value="0" data-name="">--选择接口--</option>
        {{# layui.each(d.data, function(index, a){ }}
        <option value="{{a.id}}" {{# if(d.interfaceId==a.id){ }}selected{{# }}}
                data-url="{{a.interfaceUrl}}"
                data-name="{{a.interfaceName}}">{{a.interfaceName}}</option>
        {{# }); }}
    </select>
</script>
<script id="queryDictOptionTpl" type="text/template">
    <select  name="storageType" class="form-control dict-select" style="display: inline">
        <option value="0">--选择类型--</option>
        {{# layui.each(d.data, function(index, a){ }}
        <option value="{{a.dictValue}}" data-enname="{{a.fieldEnName}}" {{# if(d.storageType==a.dictValue){ }}selected{{# }}}>{{a.dictLabel}}</option>
        {{# }); }}
    </select>
</script>
<script id="selectTpl" type="text/html">
    <select class="form-control obj{{d.prefix}}-select" data-type="{{d.type||''}}">
        <option value="0">--请选择--</option>
        {{# layui.each(d.data, function(index, a){ }}
        <option value="{{a.id}}"
                data-objectname="{{a.paramName}}"
                data-objectenname="{{a.paramEnName}}"
                data-isfieldenum="{{a.isFieldEnum}}"
                data-dictid="{{a.dictId}}"
                {{#if(a.id==d.selectId){}}selected{{# }}}
        >{{a.paramName}}
        </option>
        {{# }); }}
    </select>
</script>
<script id="selectFieldEnumTpl" type="text/html">
    <select class="form-control obj{{d.prefix}}-select contractValue" data-type="{{d.type||''}}">
        {{# layui.each(d.data, function(index, a){ }}
        <option value="{{a.dictValue}}" data-dictlabel="{{a.dictLabel}}" {{#if(a.dictValue==d.selectId){}}selected{{# }}}>{{a.dictLabel}}</option>
        {{# }); }}
    </select>
</script>
<script id="editConditionTpl" type="text/html">
    {{# layui.each(d.pageImageElementConditionList, function(index, pd){ }}
    <tr>
        <td class="delCondition"><img th:src="@{/img/Icon-jianshao.png}" class="icon-jian"></td>
        <td>{{pd.sortNum}}</td>
        <td>
            <select class="form-control branchConditionDetailDictCode-select" name="branchConditionDetailDictCode">
                <option value="1" {{#if(1==pd.branchConditionDetailDictCode){}}selected{{# }}}>字段</option>
                <option value="2" {{#if(2==pd.branchConditionDetailDictCode){}}selected{{# }}}>数据集条数</option>
                <option value="3" {{#if(3==pd.branchConditionDetailDictCode){}}selected{{# }}}>出参</option>
            </select>
        </td>
        <td>
            <select class="form-control group-select {{pd.branchConditionGroupId}}">
                <option value="1" {{#if('1'==pd.branchConditionGroupId){}}selected{{# }}}>组1</option>
                <option value="2" {{#if('2'==pd.branchConditionGroupId){}}selected{{# }}}>组2</option>
                <option value="3" {{#if('3'==pd.branchConditionGroupId){}}selected{{# }}}>组3</option>
                <option value="4" {{#if('4'==pd.branchConditionGroupId){}}selected{{# }}}>组4</option>
                <option value="5" {{#if('5'==pd.branchConditionGroupId){}}selected{{# }}}>组5</option>
            </select>
        </td>
        <td>
            <select  name="sourceType" class="form-control source-select" style="display: inline">
                <option value="0">--选择来源--</option>
                <option value="1" {{# if(1==pd.sourceType){ }}selected{{# }}}>来源接口</option>
                <option value="3" {{# if(3==pd.sourceType){ }}selected{{# }}}>来源前端本地</option>
            </select>
        </td>
        <td>
        </td>
        <td style="display: flex">
            <select class="form-control obj1-select">
                <option value="0" selected="selected">--请选择--</option>
                <option value="1" {{# if(1==pd.objectType){}}selected{{# }}}
                        style="display:{{pd.branchConditionDetailDictCode==1?'block':'none'}}"
                >字段</option>
                <option value="2" {{# if(2==pd.objectType){ }}selected{{# }}}
                        style="display:{{pd.branchConditionDetailDictCode==2?'block':'none'}}"
                >数据集</option>
                <option value="3" {{# if(3==pd.objectType){ }}selected{{# }}}
                        style="display:{{pd.branchConditionDetailDictCode==1?'block':'none'}}"
                >数据集字段</option>
            </select>
            <div class="ObjSelect" style="display: flex"></div>
        </td>
        <td>
            <select class="form-control condition-select">
                {{# layui.each(d.conditionDictList, function(index, a){ }}
                <option value="{{a.dictValue}}" {{#if(a.dictValue==pd.judgeConditionDictCode){}}selected{{# }}}>{{a.dictLabel}}</option>
                {{# }); }}
            </select>
        </td>
        <td class="contractValueTd">
            <input class="form-control contractValue" value="{{pd.contractValue||''}}"/>
        </td>
    </tr>
    {{# }); }}
</script>
<script id="rightPartTpl" type="text/html">
    <h4 class="title">{{d.pageElementName}}</h4>
    {{# layui.each(d.pageImageElementConditionList, function(i, a){ }}
    <div class="ele-condition-info">
        <h5>{{d.pindex}}.{{i+1}} {{d.pageElementName}} {{elementConditionTypes[a.elementConditionType]}}</h5>
        <div class="condition-info">
            <div class="lable">元素条件</div>
            <div style="color: #000;">
                <p>页面元素：{{d.pageElementName||''}}；需要 {{elementConditionTypes[a.elementConditionType]}}；</p>
                <p>{{a.name}}</p>
            </div>
        </div>
        {{# if(a.description){}}
        <div class="condition-info">
            <div class="lable">备        注</div>
            <div style="color: #000;">
                <p>{{a.description.replace(/;/g,'<br>')}}</p>
            </div>
        </div>
        {{# }; }}
    </div>
    {{# }); }}
</script>
<script type="text/javascript">
    var prefix = ctx + "pageImageElementRef/pageImageElementRef";
    // modal初始化
    var processConditionsModal = $("#processConditions").prop("outerHTML");
    var conditionDictList,conditionList,elementConditionTypes=['','展示','不展示','禁用'];
    $(function () {
        getConditionList();
        getConditionDictList();
    });

    $("body").on('click', ".addCondition,.editCondition", function (e) {
        $('#processConditions').modal('show');
        var data = $(this).data()
        $("input[name='pagImageElementRefId']").val(data.elementrefid);
        //名称修改
        $(".modal-body>div>label").html(`${data.name}：`);
        if($(this).attr('class').indexOf('editCondition')>=0){
            $("input[name='conditionId']").val(data.id)
            getConditionDetailList(data.id)
        }
        e.stopPropagation()
    }).on('click', ".save", function () {
        var pageImageElementCondition = {};
        pageImageElementCondition.joinCondition = $(".join-condition").val();
        pageImageElementCondition.description = $(".description").val();
        pageImageElementCondition.elementConditionType = $("select[name='elementConditionType']").val();
        pageImageElementCondition.pagImageElementRefId = $("input[name='pagImageElementRefId']").val();
        var detailList = [];
        $("#conditionTable tbody>tr").each(function (i) {
            var detail = {};
            detail.branchConditionDetailDictCode = $(this).find("select[name='branchConditionDetailDictCode']").val();
            detail.branchConditionGroupId = $(this).find(".group-select").val();
            detail.sourceType = $(this).find(".source-select").val();
            detail.storageType = $(this).find(".dict-select").val();
            // detail.isPageStore = $(this).find(".isPageStore").prop("checked")?'Y':'N'
            detail.objectCategory = $(this).find(".obj1-select").val() == 3 ? 2 : $(this).find(".obj1-select").val()
            detail.objectType = $(this).find(".obj1-select").val();
            detail.interfaceName = $(this).find(".interface-select option:selected").data("name")
            detail.interfaceUrl = $(this).find(".interface-select option:selected").data("url")
            detail.interfaceId = $(this).find(".interface-select").val();
            detail.objectId = $(this).find(".obj2-select").val();
            detail.objectName = $(this).find(".obj2-select option:selected").data("objectname");
            detail.objectEnName = $(this).find(".obj2-select option:selected").data("objectenname");
            detail.objectFieldName = $(this).find(".obj3-select option:selected").text()=='--请选择--'?'':$(this).find(".obj3-select option:selected").text();
            detail.objectFieldEnName = $(this).find(".obj3-select option:selected").data("objectenname");
            detail.paramsId = $(this).find(".obj3-select option:selected").val()||'';
            detail.judgeConditionDictCode = $(this).find(".condition-select").val();
            detail.contractValue = $(this).find(".contractValue").val();
            detail.dictName = $(this).find(".contractValue option:selected").text();
            detail.joinCondition = $(".select-group" + detail.branchConditionGroupId).val()
            detail.sortNum = $(this).find("td:eq(1)").html();
            detailList.push(detail);
        });
        pageImageElementCondition.pageImageElementConditionDetailList = detailList;
        //编辑处理
        var conditionId = $("input[name='conditionId']").val()
        conditionId && (pageImageElementCondition.id = conditionId);
        saveCondition(pageImageElementCondition);
    }).on('click', ".page-element-conditions-delete", function (e) {
        // 当前条件id
        var id = $(this).attr('id');
        // 条件删除
        $.modal.confirm("确定删除该判断条件么？", function () {
            deleteCondition(id);
        })
        e.stopPropagation();
    }).on('click', ".delCondition", function () {
        $(this).parents("tr").remove();
        for (var i = 0; i < $(".process-conditions-model tbody tr").length; i++) {
            $(`.process-conditions-model tbody tr:eq(${i}) td:nth-child(2)`).html(i + 1);
        }
    }).on('change', ".source-select", function () {
        var sourceType = $(this).val();
        //来源接口
        if(sourceType==1){
            getInterfaceList($(this))
        }else{
            queryDict($(this));
        }
    }).on('change', ".dict-select", function () {
        getInterfaceList($(this),$(this).val())
        $(this).parents("tr").find(".interface-select").val(0).trigger("change");
    }).on('change', ".interface-select", function () {
        var parentDom = $(this).parents("tr");
        var ranchConditionDetailDictCode = parentDom.find("select[name='branchConditionDetailDictCode']").val();
        ranchConditionDetailDictCode!=3 && parentDom.find(".obj1-select").trigger("change");
    }).on('change', ".obj1-select", function () {
        // 请选择不联动
        if($(this).val()!=0){
            queryFieldList($(this).val(), $(this));
            queryEnum(0,'N',$(this))
        }else{
            $(this).siblings(".ObjSelect").empty();
        }
    }).on('change', ".group-select", function () {
        addGroup();
    }).on('change', ".branchConditionDetailDictCode-select", function () {
        // 根据模式显示对象类型
        let parentObj = $(this).parents("tr");
        parentObj.find(".contractValueTd").empty();
        parentObj.find(".contractValueTd").append('<input class="form-control contractValue" value=""/>');
        if($(this).val()==3){
            parentObj.find(".obj1-select").hide();
            parentObj.find(".ObjSelect").empty();
        }else{
            parentObj.find(".obj1-select").val(0).trigger("change");
            parentObj.find(".obj1-select").show();
            if($(this).val()==1){
                parentObj.find(".obj1-select option").show();
                parentObj.find(".obj1-select option:eq(2)").hide()
            }else{
                parentObj.find(".obj1-select option").hide();
                parentObj.find(".obj1-select option:eq(0),.obj1-select option:eq(2)").show();
            }
        }
    }).on('change', ".obj2-select", function () {
        //字段类型第二个联动处理
        $(this).attr("class").indexOf("obj2-select")>=0 && $(this).data("type") == 3 && queryParamFieldList($(this).val(), $(this));
        let{isfieldenum, dictid} = $(this).find("option:selected").data();
        queryEnum(dictid,isfieldenum,$(this))
    }).on('hide.bs.modal', "#processConditions", function () {
        $(this).remove();
        $('.wrapper-content').after(window[$(this).attr("id") + 'Modal']);
    }).on("click","#treeRootByName>li",function (e) {
        //$("#treeRootByName>li").
        var rightPartTplHtml = $("#rightPartTpl").html();
        var params = Object.assign(conditionList[$(this).index()],{pindex:$(this).index()+1})
        $("input[name='showId']").val(conditionList[$(this).index()].id)
        laytpl(rightPartTplHtml).render(params, function (html) {
            $(".right-part").html('')
            $(".right-part").append(html);
        });
    }).on("click",".conditionList>li",function (e) {
        var data = $(this).data();
        var rightPartTplHtml = $("#rightPartTpl").html();
        laytpl(rightPartTplHtml).render(Object.assign(conditionList[data.pindex],{pindex:data.index+1}), function (html) {
            $(".right-part").html('')
            $(".right-part").append(html);
        });
        $("input[name='showId']").val(conditionList[data.pindex].id)
        setTimeout(function () {
            $(".ele-condition-info").hide();
            $(".ele-condition-info:eq("+data.index+")").show();
        },0)
    })

    /**
     * 添加条件
     */
    function addCondition() {
        // 添加条件
        var addConditionTplHtml = $("#addConditionTpl").html();
        laytpl(addConditionTplHtml).render({conditionDictList:conditionDictList}, function (html) {
            $(".process-conditions-model tbody").append(html);
        });
        var length = $(".process-conditions-model tbody tr").length;
        $(`.process-conditions-model tbody tr:eq(${length - 1}) td:nth-child(2)`).html(length)
        // 添加组名称
        addGroup();
    }
    /**
     * 添加组名称
     */
    function addGroup(groupSelectedList) {
        if(!groupSelectedList || groupSelectedList.length==0){
            groupSelectedList = []
            $(".group-select").each(function (e) {
                groupSelectedList.push(Object.assign({},{branchConditionGroupId:$(this).val()}))
            })
            groupSelectedList =  groupSelectedList.filter((item,index) =>{
                return groupSelectedList.findIndex(item1 =>item1.branchConditionGroupId==item.branchConditionGroupId) == index
        })
        }
        var groupInfoTplHtml = $("#groupInfoTpl").html();
        laytpl(groupInfoTplHtml).render(groupSelectedList, function (html) {
            $(".modal-footer .groupList").html(html);
        });
    }
    function obj1Select(value,parentObj){
        if(value==1){
            parentObj.find(".obj1-select option").show();
            parentObj.find(".obj1-select option:eq(2)").hide()
        }else{
            parentObj.find(".obj1-select option").hide();
            parentObj.find(".obj1-select option:eq(2)").show();
        }
    }
    // 获取接口列表
    function getInterfaceList(dom,storageType,interfaceId){
        var params = { pageId:$("input[name='pageId']").val()}
        storageType && Object.assign(params,{storageType:storageType})
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryInterfaceByPageId",
            type: "get",
            dataType: "json",
            async:false,
            data: params,
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var paramsTplHtml = $("#interfaceOptionTpl").html();
                    result.interfaceId = interfaceId||'';
                    if(storageType){
                        result.type = "storage-three";
                        laytpl(paramsTplHtml).render(result, function (html) {
                            dom.parent("td").next("td").html('')
                            dom.parent("td").next("td").html(html);
                        });
                    }else{
                        result.type = "two";
                        dom.siblings(".dict-select").remove()
                        laytpl(paramsTplHtml).render(result, function (html) {
                            dom.parent("td").next("td").html('')
                            dom.parent("td").next("td").html(html);
                        });
                        //dom.parent("td").next("td").append(`<div class="is-page-store"><input type="checkbox" class="isPageStore">是否页面store</div>`);
                    }

                }
            }
        };
        $.ajax(config);
    }
    // 获取本地类型
    function queryDict(dom,storageType) {
        var config = {
            url: ctx + "frontInterfaceFieldSource/frontInterfaceFieldSource/queryDict",
            type: "post",
            dataType: "json",
            async:false,
            data: {
                dictType:"storageType",
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var queryDictOptionTplHtml = $("#queryDictOptionTpl").html();
                    result.storageType = storageType||''
                    laytpl(queryDictOptionTplHtml).render(result, function (html) {
                        dom.siblings('.dict-select').remove()
                        dom.parent().append(html);
                    });
                    dom.parent("td").next("td").html('<select class="form-control"><option value="0" selected="selected">--请选择--</option></select>')
                }
            }
        };
        $.ajax(config);
    }
    /**
     *  查询字段接口，
     *  @params type 1-字段 2-数据集
     */
    function queryFieldList(type, dom, selectId) {
        if(type==0){
            return false;
        }
        selectId = selectId ||"";
        // 接口类型
        var interfaceId = dom.parent().prev().find(".interface-select").val();
        var config = {
            url: prefix + "/queryFieldList",
            type: "get",
            dataType: "json",
            async:false,
            data: {
                "type": type == 3 ? 2 : type,
                "interfaceId": interfaceId
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var selectTplHtml = $("#selectTpl").html();
                    result.prefix = 2
                    result.type = type
                    result.selectId= selectId
                    laytpl(selectTplHtml).render(result, function (html) {
                        dom.siblings(".ObjSelect").empty();
                        dom.siblings(".ObjSelect").append(html);
                    });
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 查询数据集字段接口
     */
    function queryParamFieldList(id, dom, selectId) {
        selectId = selectId ||"";
        let config = {
            url: prefix + "/queryParamFieldList",
            type: "get",
            dataType: "json",
            async:false,
            data: {
                "id": id
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var selectTplHtml = $("#selectTpl").html();
                    result.prefix = 3
                    result.selectId= selectId
                    laytpl(selectTplHtml).render(result, function (html) {
                        dom.siblings(".obj3-select").length==1 && dom.siblings(".obj3-select").remove()
                        dom.after(html);
                    });
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 查询判断条件
     */
    function getConditionDictList() {
        let config = {
            url: ctx + "processBranch/processBranch/conditionDictList",
            type: "get",
            dataType: "json",
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    conditionDictList = result.data
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 保存元素判断条件
     */
    function saveCondition(params) {
        var config = {
            url: prefix + "/saveCondition",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(params),
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    // 重新加载
                    getConditionList()
                    $('#processConditions').modal('hide')
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 查询元素判断条件列表
     */
    function getConditionList() {
        let config = {
            url: prefix + "/conditionList",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                "pageId": $("input[name='pageId']").val(),
                "pageImageId": $("input[name='pageImageId']").val()
            }),
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    conditionList = result.data;
                    var pageElementTplHtml = $("#pageElementTpl").html();
                    laytpl(pageElementTplHtml).render(result, function (html) {
                        $("#treeRootByName").empty();
                        $("#treeRootByName").append(html);
                    });
                    var  showId = $("input[name='showId']").val();
                    if(showId){
                        $("li[data-id='"+showId+"']").click();

                    }else{
                        $(" #treeRootByName>li:eq(0)").click()
                    }
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 查询元素元素判断条件明细
     */
    function getConditionDetailList(id){
        var config = {
            url: prefix + "/conditionDetailList",
            type: "post",
            dataType: "json",
            data: {
                "conditionId": id
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var editConditionTplHtml = $("#editConditionTpl").html();
                    laytpl(editConditionTplHtml).render({
                        conditionDictList: conditionDictList,
                        pageImageElementConditionList:result.data.pageImageElementConditionDetailList
                    }, function (html) {
                        $(".process-conditions-model tbody").append(html);
                    });
                    $("select[name='elementConditionType']").val(result.data.elementConditionType)
                    $(".description").val(result.data.description)
                    $(".join-condition").val(result.data.joinCondition);
                    var groupSelectedList = [];
                    result.data.pageImageElementConditionDetailList.forEach(function(e,index){
                        if(e.sourceType==3){
                            queryDict($("tbody tr:eq("+index+") .source-select"),e.storageType);
                        }
                        getInterfaceList($("tbody tr:eq("+index+") .source-select"),e.storageType,e.interfaceId)
                        if(e.branchConditionDetailDictCode==3){
                            $("tbody tr:eq("+index+")").find(".obj1-select").hide();
                            $("tbody tr:eq("+index+")").find(".ObjSelect").empty();
                        }else{
                            queryFieldList(e.objectType, $("tbody tr:eq("+index+") .obj1-select"),e.objectId);
                            e.objectType == 3 && queryParamFieldList(e.objectId, $("tbody tr:eq("+index+") .obj2-select"), e.paramsId);
                            var selectDom = $("tbody tr:eq("+index+") .obj2-select")
                            if(e.objectType == 3){
                                //数据集字段
                                selectDom = $("tbody tr:eq("+index+") .obj3-select");
                            }
                            let{ isfieldenum, dictid } = selectDom.find("option:selected").data();
                            queryEnum(dictid, isfieldenum,selectDom,e.contractValue);
                        }
                        groupSelectedList.push(Object.assign({},{branchConditionGroupId:e.branchConditionGroupId,joinCondition:e.joinCondition}))
                    })
                    groupSelectedList =  groupSelectedList.filter((item,index) =>{
                        return groupSelectedList.findIndex(item1 =>item1.branchConditionGroupId==item.branchConditionGroupId) == index
                    })
                    addGroup(groupSelectedList)
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 删除判断条件
     * @params conditionId 判断条件id
     */
    function deleteCondition(id) {
        let config = {
            url: prefix + "/deleteCondition",
            type: "post",
            dataType: "json",
            data: {
                "conditionId": id
            },
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    // 重新加载
                    getConditionList()
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 查询字段枚举
     */
    function queryEnum(dictId,isfieldenum,dom,selectId) {
        selectId = selectId||''
        if(isfieldenum!='Y'){
            dom.parents("td").siblings(".contractValueTd").empty();
            dom.parents("td").siblings(".contractValueTd").append('<input class="form-control contractValue" value="'+selectId+'"/>');
            return false
        }
        let config = {
            url: ctx + "processBranch/processBranch/queryEnum",
            type: "get",
            dataType: "json",
            data:{"dictId":dictId},
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function (result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var list = result.data || [];
                    if (result.code == web_status.SUCCESS) {
                        var selectFieldEnumTplHtml = $("#selectFieldEnumTpl").html();
                        result.selectId= selectId
                        laytpl(selectFieldEnumTplHtml).render(result, function (html) {
                            dom.parents("td").siblings(".contractValueTd").empty();
                            dom.parents("td").siblings(".contractValueTd").append(html);
                        });
                        return false;
                    }
                    return false;
                }
                if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                    return false;
                }
                $.modal.alertError(result.msg);
            }
        };
        $.ajax(config);
    }

    /**
     * 加载 接口参数
     */
    function loadInterfaceParams(id) {
        var config = {
            url: ctx + "interface/interface/paramList",
            type: "post",
            dataType: "json",
            data: "interfaceType=3&interfaceId="+id,
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    var resultJson = result.data;
                    if(!resultJson) {
                        return false;
                    }
                    data = resultJson || [];
                    var contentPreviewTplHtml = $("#contentPreviewTpl").html();
                    if (data.responseList != null && data.responseList.length == 2) {
                        var r1 = data.responseList[0];
                        var r2 = data.responseList[1];
                        if (r1.nodeType == 'F' && r2.nodeType == "Z") {
                            if (!r1.children) {
                                r1.children = [];
                            }
                            r1.children.push(r2);
                            var responseList = [];
                            responseList.push(r1);
                            data.responseList = responseList;
                        } else if (r2.nodeType == 'F' && r1.nodeType == "Z") {
                            if (!r2.children) {
                                r2.children = [];
                            }
                            r2.children.push(r1);
                            var responseList = [];
                            responseList.push(r2);
                            data.responseList = responseList;
                        }
                    }
                    laytpl(contentPreviewTplHtml).render(data, function (html) {
                        $("#content-preview").empty();
                        $("#content-preview").html(html);
                    });
                    $(".right-part .title").html(data.interfaceInfo.interfaceName)
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        };
        $.ajax(config);
    }

</script>
</body>
</html>
